package com.jspgou.cms.manager.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jspgou.common.hibernate3.Updater;
import com.jspgou.cms.dao.StoreTxtDao;
import com.jspgou.cms.entity.Store;
import com.jspgou.cms.entity.StoreTxt;
import com.jspgou.cms.manager.StoreTxtMng;

@Service
@Transactional
public class StoreTxtMngImpl implements StoreTxtMng {
	public StoreTxt save(StoreTxt txt, Store store){
		if (txt.isAllBlank()) {
			return null;
		} else {
			txt.setStore(store);
			txt.init();
			dao.save(txt);
			store.setStoreTxt(txt);
			return txt;
		}
	}

	public StoreTxt update(StoreTxt txt, Store store){
		StoreTxt entity = dao.findById(txt.getId());
		if (entity == null) {
			entity = save(txt, store);
			store.getStoreTxtSet().add(entity);
			return entity;
		} else {
			if (txt.isAllBlank()) {
				store.getStoreTxtSet().clear();
				return null;
			} else {
				Updater<StoreTxt> updater = new Updater<StoreTxt>(txt);
				entity = dao.updateByUpdater(updater);
				entity.blankToNull();
				return entity;
			}
		}
	}

	private StoreTxtDao dao;

	@Autowired
	public void setDao(StoreTxtDao dao) {
		this.dao = dao;
	}
}